package gui.listener;

import gui.panel.ConfigPanel;
import gui.services.ConfigService;
import gui.util.GUIUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author:feixiong
 * @Date: 2021/1/25
 */
public class ConfigListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        ConfigPanel p =ConfigPanel.instance;
        if(!GUIUtil.checkNumber(p.tfBudget,"本月预算")){
            return;
        }
        String  mysqlPath=p.tfMysqlPath.getText();
        if (0!=mysqlPath.length()){
            File commandFile =new File(mysqlPath);
            if(!commandFile.exists()){
                JOptionPane.showMessageDialog(p,"Mysql路径不正确");
                p.tfMysqlPath.grabFocus();
                return;
            }
        }
        ConfigService cs= new ConfigService();
        try {
            cs.update(ConfigService.budget,p.tfBudget.getText());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            cs.update(ConfigService.mysqlPath,mysqlPath);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        JOptionPane.showMessageDialog(p, "设置修改成功");
    }
}
